package com.cheyunkeji.er.adapter.evaluate;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.evaluate.UploadedCarActivity;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.evaluate.EvaluateCarItemInfo;
import com.cheyunkeji.er.utils.ImageLoadUtil;
import com.cheyunkeji.er.utils.TimeUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/3/23 17:50.
 */

public class UploadedCarListAdapter extends MyBaseAdapter {

    OnItemActionListener listener ;

    public UploadedCarListAdapter(ArrayList<EvaluateCarItemInfo> dataList, UploadedCarActivity context) {
        super(dataList, context);
        listener = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.er_uploaded_car_lv_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        EvaluateCarItemInfo bean = (EvaluateCarItemInfo) dataList.get(position);

        holder.tvCarBrand.setText(new StringBuffer().append(bean.getBrand_name()).append(" ").append(bean.getSeries_name()));

        if (!TextUtils.isEmpty(bean.getCover()))
            ImageLoadUtil.loadImg(context, bean.getCover(), holder.ivCar);
        else
            holder.ivCar.setImageResource(R.drawable.er_lv_default_img);

        holder.tvCarPalateNumber.setText(String.format(context.getResources().getString(R.string.car_plate_number), bean.getPlate()));
        holder.tvEvaluateDate.setText(String.format(context.getResources().getString(R.string.evaluate_date2),
                TimeUtils.millis2StringWithoutHMS(Long.valueOf(String.valueOf(bean.getDateline()) + "000"))));
        holder.ivDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDownloadClickedListener(position);
            }
        });
        holder.tv_report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onLookReportClickedListener(position);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_download)
        ImageView ivDownload;
        @BindView(R.id.iv_car)
        ImageView ivCar;
        @BindView(R.id.iv_tag)
        ImageView ivTag;
        @BindView(R.id.rl_img_display)
        RelativeLayout rlImgDisplay;
        @BindView(R.id.tv_car_brand)
        TextView tvCarBrand;
        @BindView(R.id.tv_car_palate_number)
        TextView tvCarPalateNumber;
        @BindView(R.id.tv_evaluate_date)
        TextView tvEvaluateDate;
        @BindView(R.id.rl_content)
        RelativeLayout rlContent;
        @BindView(R.id.tv_report)
        TextView tv_report;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemActionListener {
        void onDownloadClickedListener(int position);
        void onLookReportClickedListener(int position);
    }

}
